package com.gusl.uploadapi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.gusl.uploadapi.service.UploadService;
import com.gusl.uploadapi.vo.UploadedResult;

/**   
*    
* Project：uploadapi   
* Class name：ApiController   
* Class Description：   
* Author：gus   
* Create Time：2020年2月13日 下午10:29:13   
* @version        
*/

@CrossOrigin(origins = {"http://localhost:8080", "null"})
@RestController
@RequestMapping("/uploadapi")
public class ApiController
{
	@Autowired
	private UploadService uploadService;
	
	@RequestMapping(value = "/test")
	public UploadedResult test() throws Exception
	{
		UploadedResult ur = new UploadedResult();
		ur.setCode(0);
		ur.setFile("iswork!");
		ur.setSmallfile("");
		return ur;
	}
	
	@PostMapping(value = "/image")
	public UploadedResult upload(@RequestParam("file") MultipartFile file
			,@RequestParam(value="thumb",defaultValue="0",required = false) int thumb) throws Exception
	{
		if(thumb == 0)
			return uploadService.uploadImage(file,false);
		else
			return uploadService.uploadImage(file,true);
	}

	@RequestMapping(value = "/del")
	public UploadedResult del(@RequestParam(value="file",defaultValue="",required = true) String file) throws Exception
	{
		return uploadService.deleteFile(file);
	}
}
